package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.BehospitalMapper;
import com.kkb.mapper.DoctorMapper;
import com.kkb.mapper.HosregisterMapper;
import com.kkb.pojo.*;
import com.kkb.vo.QueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BeHospitalService {
    @Resource
    private BehospitalMapper behospitalMapper;
    @Resource
    private HosregisterMapper hosregisterMapper;
    @Resource
    private DoctorMapper doctorMapper;
    //查询所有住院信息
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Behospital> queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){
        BehospitalExample example = new BehospitalExample();
        BehospitalExample.Criteria criteria = example.createCriteria();
        if (vo != null){
            if (vo.getHosR_id() != null && vo.getHosR_id()>0){
                criteria.andHosR_idEqualTo(vo.getHosR_id());
            }
            if (vo.getBeginDate()!=null){
                criteria.andBeH_inTimeGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate()!=null){
                criteria.andBeH_inTimeLessThanOrEqualTo(vo.getEndDate());
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Behospital> list = behospitalMapper.selectByExample(example);
        PageHelper.startPage(pageNum,pageSize);
        List<Behospital> newList = new ArrayList<>();
        for (Behospital behospital : list) {
            Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(behospital.getHosR_id());
            Doctor doctor = doctorMapper.selectByPrimaryKey(hosregister.getD_id());
            hosregister.setDoctor(doctor);
            behospital.setHosregister(hosregister);
            if (vo.getD_name() != null && !"".equals(vo.getD_name().trim())
                    || vo.getD_keshi() != null && !"".equals(vo.getD_keshi())) {
                if (doctor.getD_name().equals(vo.getD_name()) || doctor.getD_keshi().equals(vo.getD_keshi())){
                newList.add(behospital);
                }
            }
        }
        if (newList.size()>0){
            return new PageInfo<>(newList);
        }
        return new PageInfo<>(list);
    }
    //添加住院信息
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer add(Behospital behospital){
        if (behospital != null){
            behospital.setBeH_inTime(new Date());
            behospital.setBeH_closePrice(0);
            behospital.setBeH_state(0);
        }
        return behospitalMapper.insertSelective(behospital);
    }
    //住院修改
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int update(Behospital beh){
        return behospitalMapper.updateByPrimaryKeySelective(beh);
    }
    //出院操作
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer updateState(List<Integer> list){
        int count = 0;
        for (Integer id : list) {
            Behospital behospital = behospitalMapper.selectByPrimaryKey(id);
            if (behospital.getBeH_state()!=1){
                behospital.setBeH_state(1);
                int i = behospitalMapper.updateByPrimaryKeySelective(behospital);
                if (i>0){
                    count++;
                }
            }
        }
        if (count>0){
            return 1;
        }
        return 0;
    }
    //查看详细信息
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Behospital queryById(Integer beH_id){
        Behospital behospital = behospitalMapper.selectByPrimaryKey(beH_id);
        Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(behospital.getHosR_id());
        Doctor doctor = doctorMapper.selectByPrimaryKey(hosregister.getD_id());
        hosregister.setDoctor(doctor);
        behospital.setHosregister(hosregister);
        return behospital;
    }
}
